{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| [02_lexical_analysis/03_LSTM词性标注模型.ipynb](https://github.com/shibing624/nlp-tutorial/blob/main/02_lexical_analysis/03_LSTM词性标注模型.ipynb)  | LSTM词性标注模型  |[Open In Colab](https://colab.research.google.com/github/shibing624/nlp-tutorial/blob/main/02_lexical_analysis/03_LSTM词性标注模型.ipynb) |\n",
    "\n",
    "# LSTM词性标注模型\n",
    "\n",
    "作者: Robert Guthrie\n",
    "\n",
    "在自然语言处理 (NLP, Natural Language Processing)中, 序列模型是一个核心的概念. 所谓序列模型, 即输入依赖于时间信息的模型. 一个典型\n",
    "的序列模型是隐马尔科夫模型 (HMM, Hidden Markov Model). 另一个序列模型的例子是条件随机场 (CRF, Conditional Random Field).\n",
    "\n",
    "递归神经网络是指可以保存某种状态的神经网络. 比如说, 网络上个时刻的输出可以作为下个时刻的输入, 这样信息就可以通过序列在网络中一直往后传递. 对于LSTM (Long-Short Term Memory) 来说, 序列中的每个元素都有一个相应的隐状态 $h_t$, 该隐状态原则上可以包含序列当前结点之前的任一节点的信息. 我们可以使用隐藏状态来预测语言模型中的单词, 词性标签以及其他各种各样的东西.\n",
    "\n",
    "\n",
    "## Pytorch 中的 LSTM\n",
    "\n",
    "\n",
    "开始例子之前,有几个点说明一下. Pytorch 中, LSTM 的所有的形式固定为3D 的 tensor.\n",
    "每个维度有固定的语义含义, 不能乱掉. 其中第一维是序列本身, 第二维以 mini-batch 形式\n",
    "来索引实例, 而第三维则索引输入的元素. 因为我们没有讨论过 mini-batch, 所以在这里我们\n",
    "假设第二维的维度总是1. 如果我们想在句子 \"The cow jumped\" 上运行一个序列模型, 模型的输入类似这样:\n",
    "\n",
    "\\begin{align}\\begin{bmatrix}\n",
    "   \\overbrace{q_\\text{The}}^\\text{row vector} \\\\\n",
    "   q_\\text{cow} \\\\\n",
    "   q_\\text{jumped}\n",
    "   \\end{bmatrix}\\end{align}\n",
    "\n",
    "除了有一个额外的大小为1的第二维度.\n",
    "\n",
    "此外, 你还可以向网络逐个输入序列, 在这种情况下, 第一个轴的大小也是1.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<torch._C.Generator at 0x7fe3f1752e90>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "import torch.autograd as autograd\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "\n",
    "torch.manual_seed(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[-0.0187,  0.1713, -0.2944]],\n",
      "\n",
      "        [[-0.3521,  0.1026, -0.2971]],\n",
      "\n",
      "        [[-0.3191,  0.0781, -0.1957]],\n",
      "\n",
      "        [[-0.1634,  0.0941, -0.1637]],\n",
      "\n",
      "        [[-0.3368,  0.0959, -0.0538]]], grad_fn=<StackBackward>)\n",
      "(tensor([[[-0.3368,  0.0959, -0.0538]]], grad_fn=<StackBackward>), tensor([[[-0.9825,  0.4715, -0.0633]]], grad_fn=<StackBackward>))\n"
     ]
    }
   ],
   "source": [
    "lstm = nn.LSTM(3, 3)  # 输入维度是3, 输出维度也是3\n",
    "inputs = [autograd.Variable(torch.randn((1, 3)))\n",
    "          for _ in range(5)]  # 构造一个长度为5的序列\n",
    "\n",
    "# 初始化隐藏状态\n",
    "hidden = (autograd.Variable(torch.randn(1, 1, 3)),\n",
    "          autograd.Variable(torch.randn((1, 1, 3))))\n",
    "for i in inputs:\n",
    "    # 将序列的元素逐个输入到LSTM\n",
    "    # 经过每步操作,hidden 的值包含了隐藏状态的信息\n",
    "    out, hidden = lstm(i.view(1, 1, -1), hidden)\n",
    "\n",
    "# 另外, 我们还可以一次对整个序列进行训练. LSTM 返回的第一个值表示所有时刻的隐状态值,\n",
    "# 第二个值表示最近的隐状态值 (因此下面的 \"out\"的最后一个值和 \"hidden\" 的值是一样的).\n",
    "# 之所以这样设计, 是为了通过 \"out\" 的值来获取所有的隐状态值, 而用 \"hidden\" 的值来\n",
    "# 进行序列的反向传播运算, 具体方式就是将它作为参数传入后面的 LSTM 网络.\n",
    "\n",
    "# 增加额外的第二个维度\n",
    "inputs = torch.cat(inputs).view(len(inputs), 1, -1)\n",
    "hidden = (autograd.Variable(torch.randn(1, 1, 3)), autograd.Variable(\n",
    "    torch.randn((1, 1, 3))))  # 清空输出隐状态\n",
    "out, hidden = lstm(inputs, hidden)\n",
    "print(out)\n",
    "print(hidden)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 用 LSTM 来进行词性标注\n",
    "\n",
    "\n",
    "在这部分, 我们将会使用一个 LSTM 网络来进行词性标注. 在这里我们不会用到维特比算法,\n",
    "前向后向算法或者任何类似的算法, 而是将这部分内容作为一个 (有挑战) 的练习留给读者,\n",
    "希望读者在了解了这部分的内容后能够实现如何将维特比算法应用到 LSTM 网络中来.\n",
    "\n",
    "整个模型的参数定义如下: 输入的句子定义为 $w_1, \\dots, w_M$, 其中动词定义\n",
    "为 $w_1, \\dots, w_M$, 标签集合定义为 $T$, 单词 $w_i$ 的实际\n",
    "标签为 $y_i$. 定义单词 $w_i$ 的预测标签为 $\\hat{y}_i$.\n",
    "\n",
    "这是一个结构预测模型, 我们的输出是一个序列 $\\hat{y}_1, \\dots, \\hat{y}_M$,\n",
    "其中 $\\hat{y}_i \\in T$.\n",
    "\n",
    "在进行预测时, 需将句子每个词输入到一个 LSTM 网络中. 将时刻 $i$ 的隐状态标记\n",
    "为 $h_i$. 同样地, 对每个标签赋一个独一无二的索引 (类似 word embeddings 部分\n",
    "word\\_to\\_ix 的设置). 然后就得到了 $\\hat{y}_i$ 的预测规则:\n",
    "\n",
    "\\begin{align}\\hat{y}_i = \\text{argmax}_j \\  (\\log \\text{Softmax}(Ah_i + b))_j\\end{align}\n",
    "\n",
    "即先对隐状态进行一个仿射变换, 然后计算一个对数 softmax, 最后得到的预测标签即为对数\n",
    "softmax 中最大的值对应的标签. 注意, 这也意味着 $A$ 空间的维度是 $|T|$.\n",
    "\n",
    "\n",
    "准备数据:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'The': 0, 'dog': 1, 'ate': 2, 'the': 3, 'apple': 4, 'Everybody': 5, 'read': 6, 'that': 7, 'book': 8}\n"
     ]
    }
   ],
   "source": [
    "def prepare_sequence(seq, to_ix):\n",
    "    idxs = [to_ix[w] for w in seq]\n",
    "    tensor = torch.LongTensor(idxs)\n",
    "    return autograd.Variable(tensor)\n",
    "\n",
    "\n",
    "training_data = [\n",
    "    (\"The dog ate the apple\".split(), [\"DET\", \"NN\", \"V\", \"DET\", \"NN\"]),\n",
    "    (\"Everybody read that book\".split(), [\"NN\", \"V\", \"DET\", \"NN\"])\n",
    "]\n",
    "word_to_ix = {}\n",
    "for sent, tags in training_data:\n",
    "    for word in sent:\n",
    "        if word not in word_to_ix:\n",
    "            word_to_ix[word] = len(word_to_ix)\n",
    "print(word_to_ix)\n",
    "tag_to_ix = {\"DET\": 0, \"NN\": 1, \"V\": 2}\n",
    "\n",
    "# 实际中通常使用更大的维度如32维, 64维.\n",
    "# 这里我们使用小的维度, 为了方便查看训练过程中权重的变化.\n",
    "EMBEDDING_DIM = 6\n",
    "HIDDEN_DIM = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构造模型:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LSTMTagger(nn.Module):\n",
    "\n",
    "    def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size):\n",
    "        super(LSTMTagger, self).__init__()\n",
    "        self.hidden_dim = hidden_dim\n",
    "\n",
    "        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)\n",
    "\n",
    "        #  LSTM 以 word_embeddings 作为输入, 输出维度为 hidden_dim 的隐状态值\n",
    "        self.lstm = nn.LSTM(embedding_dim, hidden_dim)\n",
    "\n",
    "        # 线性层将隐状态空间映射到标注空间\n",
    "        self.hidden2tag = nn.Linear(hidden_dim, tagset_size)\n",
    "        self.hidden = self.init_hidden()\n",
    "\n",
    "    def init_hidden(self):\n",
    "        # 开始时刻, 没有隐状态\n",
    "        # 关于维度设置的详情,请参考 Pytorch 文档\n",
    "        # 各个维度的含义是 (num_layers, minibatch_size, hidden_dim)\n",
    "        return (autograd.Variable(torch.zeros(1, 1, self.hidden_dim)),\n",
    "                autograd.Variable(torch.zeros(1, 1, self.hidden_dim)))\n",
    "\n",
    "    def forward(self, sentence):\n",
    "        embeds = self.word_embeddings(sentence)\n",
    "        lstm_out, self.hidden = self.lstm(\n",
    "            embeds.view(len(sentence), 1, -1), self.hidden)\n",
    "        tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))\n",
    "        tag_scores = F.log_softmax(tag_space, dim=1)\n",
    "        return tag_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练模型:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-1.1389, -1.2024, -0.9693],\n",
      "        [-1.1065, -1.2200, -0.9834],\n",
      "        [-1.1286, -1.2093, -0.9726],\n",
      "        [-1.1190, -1.1960, -0.9916],\n",
      "        [-1.0137, -1.2642, -1.0366]], grad_fn=<LogSoftmaxBackward>)\n",
      "tensor([[-0.0858, -2.9355, -3.5374],\n",
      "        [-5.2313, -0.0234, -4.0314],\n",
      "        [-3.9098, -4.1279, -0.0368],\n",
      "        [-0.0187, -4.7809, -4.5960],\n",
      "        [-5.8170, -0.0183, -4.1879]], grad_fn=<LogSoftmaxBackward>)\n"
     ]
    }
   ],
   "source": [
    "model = LSTMTagger(EMBEDDING_DIM, HIDDEN_DIM, len(word_to_ix), len(tag_to_ix))\n",
    "loss_function = nn.NLLLoss()\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.1)\n",
    "\n",
    "# 查看下训练前得分的值\n",
    "# 注意: 输出的 i,j 元素的值表示单词 i 的 j 标签的得分\n",
    "inputs = prepare_sequence(training_data[0][0], word_to_ix)\n",
    "tag_scores = model(inputs)\n",
    "print(tag_scores)\n",
    "\n",
    "for epoch in range(300):  # 再次说明下, 实际情况下你不会训练300个周期, 此例中我们只是构造了一些假数据\n",
    "    for sentence, tags in training_data:\n",
    "        # Step 1. 请记住 Pytorch 会累加梯度\n",
    "        # 每次训练前需要清空梯度值\n",
    "        model.zero_grad()\n",
    "\n",
    "        # 此外还需要清空 LSTM 的隐状态\n",
    "        # 将其从上个实例的历史中分离出来\n",
    "        model.hidden = model.init_hidden()\n",
    "\n",
    "        # Step 2. 准备网络输入, 将其变为词索引的 Variables 类型数据\n",
    "        sentence_in = prepare_sequence(sentence, word_to_ix)\n",
    "        targets = prepare_sequence(tags, tag_to_ix)\n",
    "\n",
    "        # Step 3. 前向传播\n",
    "        tag_scores = model(sentence_in)\n",
    "\n",
    "        # Step 4. 计算损失和梯度值, 通过调用 optimizer.step() 来更新梯度\n",
    "        loss = loss_function(tag_scores, targets)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "# 查看训练后得分的值\n",
    "inputs = prepare_sequence(training_data[0][0], word_to_ix)\n",
    "tag_scores = model(inputs)\n",
    "# 句子是 \"the dog ate the apple\", i,j 表示对于单词 i, 标签 j 的得分.\n",
    "# 我们采用得分最高的标签作为预测的标签. 从下面的输出我们可以看到, 预测得\n",
    "# 到的结果是0 1 2 0 1. 因为 索引是从0开始的, 因此第一个值0表示第一行的\n",
    "# 最大值, 第二个值1表示第二行的最大值, 以此类推. 所以最后的结果是 DET\n",
    "# NOUN VERB DET NOUN, 整个序列都是正确的!\n",
    "print(tag_scores)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 练习: 使用字符级特征来增强 LSTM 词性标注器\n",
    "\n",
    "\n",
    "在上面的例子中, 每个词都有一个词嵌入, 作为序列模型的输入. 接下来让我们使用每个的单词的\n",
    "字符级别的表达来增强词嵌入. 我们期望这个操作对结果能有显著提升, 因为像词缀这样的字符级\n",
    "信息对于词性有很大的影响. 比如说, 像包含词缀 *-ly* 的单词基本上都是被标注为副词.\n",
    "\n",
    "具体操作如下. 用 $c_w$ 来表示单词 $w$ 的字符级表达, 同之前一样, 我们使\n",
    "用 $x_w$ 来表示词嵌入. 序列模型的输入就变成了 $x_w$ 和 $c_w$ \n",
    "的拼接. 因此, 如果 $x_w$ 的维度是5, $c_w$ 的维度是3, 那么我们的 LSTM\n",
    "网络的输入维度大小就是8.\n",
    "\n",
    "为了得到字符级别的表达, 将单词的每个字符输入一个 LSTM 网络, 而 $c_w$ 则为这个\n",
    "LSTM 网络最后的隐状态. 一些提示:\n",
    "- 新模型中需要两个 LSTM, 一个跟之前一样, 用来输出词性标注的得分, 另外一个新增加的用来获取每个单词的字符级别表达.\n",
    "- 为了在字符级别上运行序列模型, 你需要用嵌入的字符来作为字符 LSTM 的输入.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本节完。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}